package com.ssy.lingxi.pay.controller.mobile;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.pay.model.vo.request.*;
import com.ssy.lingxi.pay.model.vo.response.*;
import com.ssy.lingxi.pay.service.ICreditApplyService;
import com.ssy.lingxi.pay.service.ICreditBillService;
import com.ssy.lingxi.pay.service.ICreditService;
import com.ssy.lingxi.pay.service.ICreditTradeRecordService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @menu App - 服务消费者授信相关接口
 * @author 伍将
 * @date 2020/12/30
 * @version 2.0.0
 */
@RestController
@RequestMapping("/pay/mobile/consumer/credit")
public class MobileCreditApplyController extends BaseController {

    @Resource
    private ICreditService creditService;

    @Resource
    private ICreditBillService billService;

    @Resource
    private ICreditTradeRecordService tradeRecordService;

    @Resource
    private ICreditApplyService creditApplyService;

    /**
     * 分页查询授信
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/pageCredit", method = RequestMethod.GET)
    public Wrapper<PageData<CreditQueryAppVO>> pageCredit(HttpServletRequest httpRequest, @Valid PageQueryCreditVO pageVO) {
        return creditService.pageCreditByApp(getSysUser(httpRequest), pageVO);
    }

    /**
     * 查询授信详情
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/getCreditDetail", method = RequestMethod.GET)
    public Wrapper<CreditDetailVO> getDetail(HttpServletRequest httpRequest, Long id) {
        return creditService.getDetail(getSysUser(httpRequest), id, false);
    }

    /**
     * 查询账单详情
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/getCreditBillDetail", method = RequestMethod.GET)
    public Wrapper<CreditBillDetailVO> getBillDetail(HttpServletRequest httpRequest, Long id) {
        getSysUser(httpRequest);
        return billService.getDetailByMobile(id);
    }

    /**
     * 分页查询交易记录
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/pageCreditTradeRecord", method = RequestMethod.GET)
    public Wrapper<PageData<CreditTradeRecordPageQueryAppVO>> pageTradeRecord(@Valid PageQueryCreditTradeRecordVO pageVO) {
        return tradeRecordService.pageTradeRecordByApp(pageVO);
    }

    /**
     * 分页查询逾期记录
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/pageCreditOverdue", method = RequestMethod.GET)
    public Wrapper<PageData<CreditOverduePageQueryAppVO>> pageOverdue(PageQueryCreditOverdueVO pageVO) {
        return billService.pageOverdueByApp(pageVO);
    }

    /**
     * 立即还款
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/creditRepay", method = RequestMethod.POST)
    public Wrapper<CreditRepayResultVO> creditRepay(HttpServletRequest httpRequest, @RequestBody @Valid CreditRepayVO vo) {
        return creditService.repay(getSysUser(httpRequest), vo, httpRequest);
    }

    /**
     * 查询还款结果
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/getCreditRepayResult", method = RequestMethod.GET)
    public Wrapper<CreditRepayResultVO> getCreditRepayResult(HttpServletRequest httpRequest, Long recordId) {
        return creditService.getCreditRepayResult(getSysUser(httpRequest), recordId);
    }

    /**
     * 分页查询授信申请
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/pageCreditApply", method = RequestMethod.GET)
    public Wrapper<PageData<CreditApplyPageQueryVO>> pageCreditApply(HttpServletRequest httpRequest, PageQueryCreditApplyVO pageVO) {
        return creditApplyService.pageApplyByLower(getSysUser(httpRequest), pageVO);
    }

    /**
     * 保存授信申请
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/addCreditApply", method = RequestMethod.POST)
    public Wrapper<Long> saveCreditApply(HttpServletRequest httpRequest, @RequestBody @Valid CreditApplyAddVO addVO) {
        return creditApplyService.add(getSysUser(httpRequest), addVO);
    }

    /**
     * 获取授信申请详情
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/getApplyDetail", method = RequestMethod.GET)
    Wrapper<CreditApplyDetailVO> getApplyDetail(HttpServletRequest httpRequest, @Valid CreditApplyGetDetailVO getDetailVO) {
        return creditApplyService.getDetail(getSysUser(httpRequest), getDetailVO, false);
    }

    /**
     * 删除授信申请
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping(value = "/deleteCreditApply", method = RequestMethod.POST)
    public Wrapper<Void> delete(HttpServletRequest httpRequest, @RequestBody CreditApplySubmitVO vo) {
        return creditApplyService.delete(getSysUser(httpRequest), vo);
    }

    /**
     * 提交授信申请
     * @author 伍将
     * @date 2020/12/30
     **/
    @RequestMapping("/submitCreditApply")
    public Wrapper<Void> submitCreditApply(HttpServletRequest httpRequest, @RequestBody CreditApplySubmitVO submitVO) {
        return creditApplyService.submit(getSysUser(httpRequest), submitVO);
    }

    /**
     * 获取可用余额
     * @author 伍将
     * @date 2021/1/30
     **/
    @RequestMapping("/getCanUseQuota")
    public Wrapper<Double> getCanUseQuota(HttpServletRequest httpRequest) {
        return creditService.getCanUseQuota(getSysUser(httpRequest));
    }

    /**
     * 获取授信
     * @author 伍将
     * @date 2021/2/23
     **/
    @RequestMapping(value = "/getCredit", method = RequestMethod.GET)
    public Wrapper<CreditVO> getCredit(HttpServletRequest httpRequest, @Valid CreditVORequest request) {
        return creditService.getCredit(getSysUser(httpRequest), request);
    }

    /**
     * 获取会员授信
     * @author 伍将
     * @date 2021/2/23
     **/
    @RequestMapping(value = "/getMemberCredit", method = RequestMethod.GET)
    public Wrapper<CreditVO> getMemberCredit(HttpServletRequest httpRequest, @Valid CreditRequestVO request) {
        return creditService.getMemberCredit(getSysUser(httpRequest), request);
    }

    /**
     * 检查是否已申请授信
     * @author 伍将
     * @date 2021/3/9
     **/
    @RequestMapping(value = "/existApplyCredit", method = RequestMethod.GET)
    public Wrapper<Boolean> existApplyCredit(HttpServletRequest httpRequest) {
        return creditService.existApplyCredit(getSysUser(httpRequest));
    }

    /**
     * 查询授信调额记录
     * @author 伍将
     * @date 2021/3/9
     **/
    @RequestMapping(value = "/getAdjustQuotaList", method = RequestMethod.GET)
    public Wrapper<List<CreditAdjustQuotaRecordVO>> getAdjustQuotaList(HttpServletRequest httpRequest, Long creditId) {
        return creditApplyService.getAdjustQuotaList(getSysUser(httpRequest), creditId);
    }
}
